package assignment1.eventplan.request;


import android.support.annotation.StringDef;
import android.support.v4.util.Pools;

import com.google.android.gms.maps.model.LatLng;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import assignment1.eventplan.R;
import assignment1.eventplan.application.MyApplication;

public class MatrixRequestBuilder {
    //root url ?origins=41.43206,-81.38992&destinations=Vancouver+BC&mode=bicycling&language=en

    /**
     * mode:
     * 1.driving（default）
     * 2.walking
     * 3.bicycling
     * 4.transit
     */

    @StringDef({Mode.BICYCLING, Mode.DRIVING, Mode.WALKING, Mode.TRANSIT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Mode {
        String DRIVING = "driving";
        String WALKING = "walking";
        String BICYCLING = "bicycling";
        String TRANSIT = "transit";
    }

    private static final Pools.SynchronizedPool<MatrixRequestBuilder> sPool
            = new Pools.SynchronizedPool<MatrixRequestBuilder>(
            10);

    private final String host;
    private final String key;

    @Mode
    private String mode;

    private String destinationAddress;
    private String originAddress;

    public static MatrixRequestBuilder obtain() {
        MatrixRequestBuilder builder = sPool.acquire();
        if (null == builder) {
            builder = new MatrixRequestBuilder();
        }
        return builder;
    }

    private MatrixRequestBuilder() {
        @Mode String mode = MyApplication.get().getString(R.string.google_map_distance_martix_default_mode);
        this.host = MyApplication.get().getString(R.string.google_map_distance_martix_host);
        this.key = MyApplication.get().getString(R.string.google_maps_key);
        this.mode = mode;
    }


    public MatrixRequestBuilder mode(@Mode String mode) {
        this.mode = mode;
        return this;
    }


    public MatrixRequestBuilder originAddress(String originAddress) {
        this.originAddress = originAddress;
        return this;
    }

    public MatrixRequestBuilder originAddress(LatLng latLng) {
        this.originAddress = String.valueOf(latLng.latitude) + "," + latLng.longitude;
        return this;
    }


    public MatrixRequestBuilder destinationAddress(String destinationAddress) {
        this.destinationAddress = destinationAddress;
        return this;
    }

    public MatrixRequestBuilder destinationAddress(LatLng latLng) {
        this.destinationAddress = String.valueOf(latLng.latitude) + "," + latLng.longitude;
        return this;
    }


    public void request(long id, Request.RequestListener listener) {
        Request request = new Request(generateHost());
        request.setRequestId(id);
        request.addListener(listener);
        RequestEngine.get().request(request);
        sPool.release(this);
    }

    private String generateHost() {
        return host
                + "origins=" + originAddress
                + "&destinations=" + destinationAddress
                + "&mode=" + mode
                + "&language=en"
                + "&key=" + key;
    }

}
